from ant import Ant 
import urllib2,urllib
from parsingant import parser
import supplyingant
import threading,Queue,random,time,sys
class retriever(threading.Thread,Ant):
	pile = Queue.Queue(5)
	def run(self):
		while True:
			twig = retriever.pile.get()
			#print("retriever")
			Ant.leave("retriever")
			temp=""
			temp=self.retrieve(twig)
			if(temp!=""):
				parser.pile.put(temp)
			retriever.pile.task_done()
			Ant.comehome("retriever")
	def retrieve(self,pt):
                
		self.artist=urllib.quote_plus(pt['artist_tag'].encode('utf-8').rstrip())
		
                if("a" in Ant.__commandstring__):
			self.track=urllib.quote_plus(pt['track_tag'].encode('utf-8').rstrip())
			try:
                                pt['info_page']=urllib2.urlopen(Ant.api_url+"track.getinfo"+Ant.autocorrect+"artist="+self.artist+"&track="+self.track,None,10).read()
                        except:
                                return ""

		if("g" in pt['command_string']):
                        try:
                                pt['genre_page']=urllib2.urlopen(Ant.api_url+"artist.getTopTags"+Ant.autocorrect+"artist="+self.artist,None,10).read()
                        except:
                                print("HTTP Error")
                                supplyingant.supplier.fail(pt['artist_tag'])
                                return ""
		return pt
